package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.BeeFramework.Utils.Utils;
import com.BeeFramework.activity.BaseActivity;
import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.model.UserBalanceModel;
import com.example.moshudriver.model.UserModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.USER;
import com.example.moshudriver.protocol.userverifycodeResponse;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.external.androidquery.callback.AjaxStatus;

//编辑手机号
public class C5_EditMobileActivity extends BaseActivity implements
		BusinessResponse, View.OnClickListener {
	private TextView mTitle;
	private ImageView mBack;
	// private ImageView mTopViewRightImage;
	private Button mSave;
	private SharedPreferences mShared;
	private EditText mMobile;
	private UserModel mUserModel;
	private Button mGetVerifyCodeAgain;
	private EditText mVerifyCode;
	private USER user;
	private UserBalanceModel mUserBalance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.c5_edit_mobile);
		mTitle = (TextView) findViewById(R.id.top_view_title);
		mTitle.setText(getString(R.string.edit_mobile));
		mBack = (ImageView) findViewById(R.id.top_view_back);
		mMobile = (EditText) findViewById(R.id.et_mobile);
		mGetVerifyCodeAgain = (Button) findViewById(R.id.btn_get_verify_code_again);
		mVerifyCode = (EditText) findViewById(R.id.c5_et_verify_code);

		mShared = getSharedPreferences(O2OMobileAppConst.USERINFO, 0);
		String userStr = mShared.getString("user", "");

		try {
			if (userStr != null) {
				JSONObject userJson = new JSONObject(userStr);
				user = new USER();
				user.fromJson(userJson);
				mMobile.setText(user.mobile_phone);
				mMobile.setSelection(user.mobile_phone.length());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		mUserBalance = new UserBalanceModel(this);
		mUserBalance.addResponseListener(this);
		mUserModel = new UserModel(this);
		mUserModel.addResponseListener(this);
		mSave = (Button) findViewById(R.id.btn_save);
		mBack.setOnClickListener(this);
		mSave.setOnClickListener(this);
		mGetVerifyCodeAgain.setOnClickListener(this);
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_VERIFYCODE)) {
			userverifycodeResponse response = new userverifycodeResponse();
			response.fromJson(jo);
			if (response.succeed == 1) {
				TimeCount mTime = new TimeCount(60000, 1000);// 构造CountDownTimer对象
				mTime.start();
			} else {
				mMobile.requestFocus();
			}
		} else if (url.endsWith(ApiInterface.USER_CHANGE_PROFILE)) {
			ToastView toast = new ToastView(C5_EditMobileActivity.this,
					getString(R.string.edit_mobile_phone_success));
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			mUserBalance.signout();
			finish();
		}
	}

	@Override
	public void onClick(View v) {
		String mobileNum = mMobile.getText().toString().trim();
		// 验证码
		String verify_code = mVerifyCode.getText().toString().trim();
		switch (v.getId()) {
		case R.id.top_view_back:
			finish();
			break;
		case R.id.btn_get_verify_code_again:
			// 获取短信验证
			if ("".equals(mobileNum)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else if (mobileNum.length() < 11 || !Utils.isMobile(mobileNum)) {
				ToastView toast = new ToastView(this,
						getString(R.string.wrong_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else if (mobileNum.equals(user.mobile_phone)) {
				ToastView toast = new ToastView(this,
						getString(R.string.verify_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else {// 这个地方获取短信验证码并且发送

				mUserModel.getVerifyCode(mobileNum);// 获取短信验证码
				ToastView toast = new ToastView(this, "短信验证码已经发送");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.requestFocus();
			}
			break;
		case R.id.btn_save:
			if ("".equals(mobileNum)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else if (mobileNum.length() < 11 || !Utils.isMobile(mobileNum)) {
				ToastView toast = new ToastView(this,
						getString(R.string.wrong_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mMobile.requestFocus();
			} else if (mobileNum.equals(user.mobile_phone)) {
				ToastView toast = new ToastView(this,
						getString(R.string.verify_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else if ("".equals(verify_code)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_verify_code));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				// mVerifyCode.setText("");
				mVerifyCode.requestFocus();
			} else if (verify_code.length() < 6) {
				ToastView toast = new ToastView(this, "输入6位的短信验证码");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.requestFocus();
			} else if (!user.mobile_phone.equals(mobileNum)) {
				// 验证手机号
				mUserModel.valid_verifycode(mMobile.getText().toString(),
						verify_code);

			} else {
				mUserModel.changeMobile(mobileNum);
			}
			// 测试
			// else if ("123456".equals(verify_code)) {
			// mUserModel.changeMobile(mobileNum);
			// }
		}
	}

	/* 定义一个倒计时的内部类 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			mGetVerifyCodeAgain
					.setText(getString(R.string.get_verify_code_again));
			mGetVerifyCodeAgain.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			mGetVerifyCodeAgain.setClickable(false);
			mGetVerifyCodeAgain.setText(millisUntilFinished / 1000
					+ getString(R.string.resend_after));
		}
	}
}